<?php

namespace App\HttpController\Wx;


use App\Common\Log;
use App\Common\WX\WxCenter;
use App\HttpController\ControllerBase;
use App\Model\t_s_accsuper_config;
use EasySwoole\WeChat\Bean\OfficialAccount\AccessCheck;
use EasySwoole\WeChat\Bean\OfficialAccount\Message\Text;
use EasySwoole\WeChat\Bean\OfficialAccount\RequestConst;
use EasySwoole\WeChat\Bean\OfficialAccount\RequestMsg;
use EasySwoole\WeChat\WeChat;
use function foo\func;
use XWX\Common\H;

/**
 * 接收微信信息
 * http://double123456.vicp.cc/wx/center?id=1
 * http://center.php.x.siyoumi.com/wx/center?id=1
 *
 * Class center
 * @package App\HttpController\wx
 */
class Center extends ControllerBase
{
    /**
     * 获取处理微信回复对象
     *
     * @return WeChat
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    private function funcGetAppWechat()
    {
        $app = $this->context()->get(__METHOD__);
        if ($app == null)
        {
            $acc_id = $this->reqParam('id');

            /** @var t_s_accsuper_config $entity_accf */
            $entity_accf = t_s_accsuper_config::funcLoad($this->db(), $acc_id);

            $app = new WeChat();
            $app->officialAccount()->getConfig()
                ->setAppId($entity_accf->aconfig_app_id)
                ->setAppSecret($entity_accf->aconfig_app_secret)
                ->setToken($entity_accf->aconfig_token);

            $this->context()->set(__METHOD__, $app);
            $this->context()->funcSetX($entity_accf->funcGetX());
        }


        return $app;
    }


    public function index()
    {
        $server = $this->funcGetAppWechat()->officialAccount()->server();


        Log::funcLogDebug(__METHOD__, $this->reqParam());

        $data_access = new AccessCheck($this->request()->getQueryParams());
        $is_check_server = $server->accessCheck($data_access);
        if (!$is_check_server)
        {
            Log::funcLogErr(__METHOD__, '签名不通过');

            $this->response()->write('');
            return;
        }


        $server->preCall(function (RequestMsg $msg)
        {
            //返回null不作处理
            $res = $this->funcHandler($msg);
            if ($res != null)
            {
                return $res;
            }
        });


        $write_content = '';
        if ($this->request()->getMethod() == 'GET')
        {
            $write_content = $data_access->getEchostr();
        }
        else
        {
            $raw_content = $this->request()->getSwooleRequest()->rawcontent();
            Log::funcLogDebug('raw_content', $raw_content);

            $res = $server->parserRequest($raw_content);
            if (is_string($res))
            {
                $write_content = $res;
            }
        }

        Log::funcLogInfo(__METHOD__, $write_content);


        $this->response()->write($write_content);
    }


    /**
     * 回复处理
     * null:不作处理
     *
     * @param RequestMsg $msg
     * @return Text|null
     */
    function funcHandler(RequestMsg $msg)
    {
        Log::funcLogDebug(__METHOD__);

        $wx_center = new WxCenter($msg);
        $reply = $wx_center->funcHandle();


        return $reply;
    }
}